package dao;
import java.sql.*;

public class Dao {
	private static String driver="com.mysql.jdbc.Driver";
	private static String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
	private static String name="root";
	private static String pass="123456";
	private Connection con;
	private Statement stmt;
	private ResultSet rs;
	private int message=0;
	//连接数据库
	private void conDB(){
		try{
			Class.forName(driver);
			con=DriverManager.getConnection(url,name,pass);
			stmt=con.createStatement();
		}
		catch(ClassNotFoundException e){
			System.out.println("SQL Exception:"+e.getLocalizedMessage());
		}
		catch(SQLException e){
			System.out.println(e);
		}
	}
	//查询操作
	public String[][] doSelect(String sql){
		conDB();
		String[][]result=null;
		try{
			rs=stmt.executeQuery(sql);
			rs.last();
			int x=rs.getRow();
			ResultSetMetaData rsm=rs.getMetaData();
			int y=rsm.getColumnCount();
			result=new String[x][y];
			rs.beforeFirst();
			int i=0;
			int j=0;
			while(rs.next()){
				j=0;
				while(j<y){
					result[i][j]=rs.getString(j+1);
					j++;
				}
				i++;
			}
			con.close();
			stmt.close();
			rs.close();
		}
		catch(SQLException e){
			System.out.println(e);
		}
		return result;
	}
	//其他数据库操作
	public void doSql(String sql){
		conDB();
		try {
			stmt.execute(sql);
			message=1;
			con.close();
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}